package org.jsserv.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.log4j.Logger;
import org.springframework.core.io.ClassPathResource;

/**
 * Some tool functions for coping with webapp-relative paths.
 * @author shelmberger
 *
 */
public class WebAppPathUtil
{
    protected static final Logger log = Logger.getLogger(WebAppPathUtil.class);

    private WebAppPathUtil()
    {
        // do not instantiate
    }

    /**
     * Returns the real file system path of the given webapp relative URI.
     * @param webAppPath webApplikations-relativer Pfad (z.b. "/WEB-INF/web.xml")
     * @return Datei-Pfad
     */
    public static String getRealPath(String webAppPath)
    {
        try
        {
            return getURL(webAppPath).toURI().getPath();
        }
        catch (URISyntaxException e)
        {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Returns a file:// URL  of the  given webapp relative URI.
     * @param webAppPath webapp-relative path (e.g. "/WEB-INF/web.xml")
     * @return file: URL
     */
    public static URL getURL(String webAppPath)
    {
        try
        {
            
            int jsPos=webAppPath.indexOf(";jsessionid=");
            if (jsPos >= 0)
            {
                webAppPath=webAppPath.substring(0, jsPos);
            }
            
            String res = WebAppPathUtil.class.getName().replace(".", "/")
                    + ".class";
            ClassPathResource classPathResource = new ClassPathResource(res);
            URL url = classPathResource.getURL();

            String webURL = url.toExternalForm();

            int webInfPos = webURL.indexOf("/WEB-INF");
            if (webInfPos < 0)
            {
                try
                {                    
                    // eclipse and ant have the current dir set to the project for tests etc.
                    String path=("WebContent/"+webAppPath).replace('/', File.separatorChar);
                    return new File(path).getAbsoluteFile().toURL();
                }
                catch(Exception  e)
                {
                    log.error(e);
                }
                throw new IllegalStateException("Class is neither in WEB-INF folder nor in an Eclipse WTP project/ant setup");
            }
            
            return new URL(webURL.substring(0, webInfPos)+webAppPath);
        }
        catch (MalformedURLException e)
        {
            throw new IllegalStateException(e);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
   }
    
    public static Reader getReader(String uri)
    {
        try
        {
            return new FileReader(getRealPath(uri));
        }
        catch (FileNotFoundException e)
        {
            log.error("",e);
            return null;
        }
    }
}
